//- Copyright (c) 2016 Lucky Byte, Inc.
extends ../table

append scripts
  script(src="/js/merch/index.js")

block form_items
  .field.two.wide
    .ui.labeled.dropdown.selection.input.inside
      input(type="hidden" name="inside" value="#{inside || ''}")
      i.icon.dropdown
      .default.text 所有商户
      .menu
        .item(data-value="") 所有商户
        .item(data-value="true") 平台商户
        .item(data-value="false") 外联商户

  .field.two.wide
    .ui.labeled.dropdown.selection.input.chnl
      input(type="hidden" name="chnl" value="#{chnl || ''}")
      i.icon.dropdown
      .default.text 所有渠道
      .menu
        .item(data-value="") 所有渠道
        - for (var k in constant.merch_chnls)
          .item(data-value="#{k}")= constant.merch_chnls[k]

  .field.nine.wide
    .ui.buttons.right.floated.small.compact#primary
      if page_perms.new
        button#new.ui.green.button(type="button") 新增商户
      else
        button#new.ui.green.icon.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 新增商户

      if page_perms.import
        button#import.ui.red.button(type="button") 导入商户
      else
        button#import.ui.red.icon.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 导入商户

      if page_perms.export
        button#export.ui.blue.button(type="button") 导出商户
      else
        button#export.ui.blue.icon.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 导出商户

      if page_perms.batch
        button#batch.ui.purple.button(type="button") 批量处理
      else
        button#batch.ui.purple.icon.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 批量处理

    .ui.buttons.right.floated.small.compact#secondary(style="display:none;")
      if page_perms.batch && page_perms.check
        button#batch_check.ui.green.button.disabled(type="button") 批量复核
      else
        button#batch_check.ui.green.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 批量复核

      if page_perms.batch && page_perms.disable
        button#batch_disable.ui.orange.button.disabled(type="button") 批量禁用
      else
        button#batch_disable.ui.orange.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 批量禁用

      if page_perms.batch && page_perms.disable
        button#batch_enable.ui.teal.button.disabled(type="button") 批量启用
      else
        button#batch_enable.ui.teal.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 批量启用

      if page_perms.batch && page_perms.delete
        button#batch_delete.ui.red.button.disabled(type="button") 批量删除
      else
        button#batch_delete.ui.red.button.disabled(type="button")
          i.icon.ban(style="margin-top:1px; margin-right:2px;")
          | 批量删除

      button#batch_exit.ui.purple.button(type="button") 退出批量处理

block thead
  tr
    th.center.aligned.collapsing#batch(style="display:none;")
    th.center.aligned.collapsing#status
    th.center.aligned.collapsing 序 号
    th.center.aligned.collapsing 类型
    th.center.aligned.collapsing 渠 道
    th.center.aligned 商户编号
    th.center.aligned 商户名称
    th.center.aligned 联系人
    th.center.aligned 联系电话
    th.center.aligned 创建时间
    th.center.aligned 引用数
    th.center.aligned 终端数
    th.center.aligned 备 注

block tbody
  each record, index in records
    - var td_class = record.disabled ? 'disabled' : ''
    - var tr_class = record.ex ? 'warning' : ''
    if record.ex && record.inputer == userinfo.uuid
      - tr_class = 'positive'
    else if record.chnl == 0
      - td_class = "positive"

    tr(class="#{tr_class}")
      -
        var batch_editable = false
        var batch_checkable = false
        var batch_disable = false
        var batch_enable = false
        var batch_deletable = false
        var batch_deletable_ex = false
        if (!record.ex && !record.disabled && record.chnl != 0 && !record.inside)
          batch_editable = true
        if (record.ex && record.inputer != userinfo.uuid)
          batch_checkable = true
        if (!record.ex && !record.disabled)
          batch_disable = true
        if (!record.ex && record.disabled)
          batch_enable = true
        if (!record.ex)
          batch_deletable = true
        if (record.ex && record.inputer == userinfo.uuid)
          batch_deletable_ex = true

      td.center.aligned.collapsing#batch(
        style="padding-bottom:2px; padding-right: 0px; display:none;")
        .ui.checkbox#batch(data-href=""
          data-batch-editable="#{batch_editable}"
          data-batch-checkable="#{batch_checkable}"
          data-batch-disable="#{batch_disable}"
          data-batch-enable="#{batch_enable}"
          data-batch-deletable="#{batch_deletable}"
          data-batch-deletable-ex="#{batch_deletable_ex}"
          data-serial="#{record.serial}" data-uuid="#{record.uuid}"
          data-ex="#{record.ex}")
          input(type="checkbox")
          label 

      td.center.aligned.collapsing#status
        if record.ex
          if record.inputer == userinfo.uuid
            if record.checked
            case record.action
              when 1
                if record.checked
                  i.icon.wait.grey(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您添加的该商户信息通过复核，预计生效时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}"
                    data-variation="small very wide")
                else if record.rejected
                  i.icon.plus.link.red#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您添加的该商户信息，但在复核时遭拒绝，您可以修改后重新提交复核"
                    data-variation="small very wide")
                else
                  i.icon.plus.link.green#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您添加的该商户信息，但尚未通过复核，复核前您可以再次对信息进行修改"
                    data-variation="small very wide")
              when 2
                if record.checked
                  i.icon.wait.grey(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您修改的该商户信息通过复核，预计生效时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}"
                    data-variation="small very wide")
                else if record.rejected
                  i.icon.write.link.red#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您修改过该商户信息，但在复核时遭拒绝，您可以修改后重新提交复核"
                    data-variation="small very wide")
                else
                  i.icon.write.link.blue#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您修改过该商户信息，但尚未通过复核，复核前您可以再次对信息进行修改"
                    data-variation="small very wide")
              when 3
                if record.checked
                  i.icon.wait.grey(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您删除的该商户信息通过复核，预计删除时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}"
                    data-variation="small very wide")
                else if record.rejected
                  i.icon.minus.link.red#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您删除了该商户信息，但在复核时遭拒绝，您可以修改后重新提交复核"
                    data-variation="small very wide")
                else
                  i.icon.minus.link.orange#input(
                    data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                    style="margin-right:0px;"
                    data-content="您删除了该商户信息，但尚未通过复核，复核前您可以删除此记录进行撤销"
                    data-variation="small very wide")
          else if record.checked && record.checker == userinfo.uuid
            - var color = 'green'
            case record.action
              when 1
                i.icon.wait.link.green#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="您确认过可以新增该商户信息，但尚未生效，生效时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}，生效前您可以再次进行复核"
                  data-variation="small very wide")
              when 2
                i.icon.wait.link.blue#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="您确认过可以变更该商户信息，但尚未生效，生效时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}，生效前您可以再次进行复核"
                  data-variation="small very wide")
              when 3
                i.icon.wait.link.red#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="您确认过可以删除该商户信息，但尚未生效，生效时间为 #{record.effect_time.format('YYYY-MM-DD HH:mm:ss')}，生效前您可以再次进行复核"
                  data-variation="small very wide")
          else
            case record.action
              when 1
                i.icon.checkmark.box.link.green#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="该商户信息为新增记录，录入用户为 #{record.input_user}，请复核"
                  data-variation="small wide")
              when 2
                i.icon.checkmark.box.link.blue#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="该商户信息被用户 #{record.input_user} 变更，请复核"
                  data-variation="small wide")
              when 3
                i.icon.checkmark.box.link.red#check(
                  data-serial="#{record.serial}" data-uuid="#{record.uuid}"
                  data-action="#{record.action}"
                  style="margin-right:0px;"
                  data-content="该商户信息被用户 #{record.input_user} 删除，请复核"
                  data-variation="small wide")
        else if record.disabled
          i.icon.lock.link.orange(
            style="margin-right:0px;" data-content="该商户已被禁用"
            data-variation="small")
        else if record.chnl == 0
          i.icon.sitemap.pink.link#settl(
            style="margin-right:0px;"
            data-uuid="#{record.uuid}"
            data-content="该商户为清算商户，点击查看商户列表"
            data-variation="small")
        else if record.inside
          i.icon.recycle.link.grey(
            style="margin-right:0px;"
            data-content="平台商户" data-variation="small")
        else
          i.icon.random.link.grey(
            style="margin-right:0px;"
            data-content="外联商户" data-variation="small")

      td.left.aligned.collapsing
        if record.ex
          i.icon.caret.right.link.detail.expand
          = record.ref_serial || record.serial
        else
          if page_perms.edit || page_perms.disable
            i.icon.caret.right.link.detail.expand
            a(href="/merch/edit/#{record.serial}")= record.serial
          else
            i.icon.caret.right.link.detail.expand
            = record.serial
      td.center.aligned.collapsing(class="#{td_class}")
        = record.inside ? '平台' : '外联'
      td.center.aligned.collapsing(class="#{td_class}")
        = constant.merch_chnls[record.chnl] || '未知'
      td.center.aligned.collapsing(class="#{td_class}")= record.merno
      td.center.aligned.collapsing(class="#{td_class}")= record.name
      td.center.aligned.collapsing(class="#{td_class}")= record.contact
      td.center.aligned.collapsing(class="#{td_class}")= record.mobile
      td.center.aligned.collapsing(class="#{td_class}")
        = record.ctime.format('YYYY-MM-DD HH:mm:ss')
      td.center.aligned.collapsing(class="#{td_class}")= record.n_refs || 0
      td.center.aligned.collapsing(class="#{td_class}")= record.n_terms || 0
      if record.rejected
        td(class="error") 复核拒绝，原因：#{record.reject_reason}
      else
        td(class="#{td_class}")= record.notes

    tr.detail(style="display: none;")
      td.right.aligned.warning(colspan="1") 详细信息
      td(colspan="12"
        style="padding-top:0px; padding-bottom:0px; padding-right:1px;")
        include detail
